package com.work.csms.service;

import com.work.csms.dao.User;
import com.work.csms.mapper.UserMapper;
import com.work.csms.pojo.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    public List<User> queryAllUser() throws Exception {
        return userMapper.queryAllUser();
    }

    public Map<String, Object> page(User user, PageParam pageParam) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Integer count = userMapper.count(user.getName());
        List<User> page = userMapper.page(user.getName(), (pageParam.getPageNo() - 1) * pageParam.getPageSize(), pageParam.getPageSize());
        result.put("count", count);
        result.put("pageSize", pageParam.getPageSize());
        result.put("pageNo", pageParam.getPageNo());
        result.put("data", page);
        return result;
    }

    public User getUserById(Integer userid) throws Exception {
        return userMapper.getUserById(userid);
    }

    public User getUserByUserName(String username) throws Exception {
        return userMapper.getUserByUserName(username);
    }

    public Integer login(String username, String password) throws Exception {
        return userMapper.login(username, password);
    }

    public Integer hasUsername(String username) throws Exception {
        return userMapper.hasUsername(username);
    }

    public void regist(String username, String password, Integer usertype) throws Exception {
        userMapper.regist(username, password, usertype);
    }

    public void regist(String username, String name, String telephone, Integer usertype) throws Exception {
        userMapper.regist2(username, name, telephone, usertype);
    }

    public void updateUser(Integer userid, String username, String name, String password, String telephone,
                           String email) throws Exception {
        userMapper.updateUser(userid, username, name, password, telephone, email);
    }

    public void updateMoneyByUserid(Integer userid, Integer money) throws Exception {
        userMapper.updateMoneyByUserid(userid, money);
    }

    public void save(User user) throws Exception {
        userMapper.save(user);
    }

    public void deleted(Integer userid) throws Exception {
        userMapper.deleted(userid);
    }
}
